package com.jeasonzhao.model;

import java.lang.reflect.Field;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

import com.jeasonzhao.commons.annotation.Dictionary;
import com.jeasonzhao.commons.annotation.NameField;
import com.jeasonzhao.commons.annotation.Title;
import com.jeasonzhao.commons.annotation.TreeNodeId;
import com.jeasonzhao.commons.annotation.TreeNodeParentId;
import com.jeasonzhao.commons.annotation.TreeNodeText;
import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.annotation.SortField;

public class FieldAppendix
{
    private Field field = null;
    private String title = null;
    private String width = null;
    private boolean enumName=true;

    private String dictTypeId = null;
    private String dictCategory = null;

    //Generate dictionary from special class
    private String pojo = null;

    private String dictItems = null;
    private TreeNodeTypes treeNodeType = TreeNodeTypes.None;
    private boolean sortField=false;

    private boolean dictionaryNode = false;

    public FieldAppendix(MappedFieldItem item)
    {
        if(null == item || null == item.getField())
        {
            return;
        }
        this.field = item.getField();
        dictionaryNode = false;
        this.sortField = this.field.getAnnotation(SortField.class)!=null;
        TreeNodeId treeId = this.field.getAnnotation(TreeNodeId.class);
        TreeNodeParentId treePId = this.field.getAnnotation(TreeNodeParentId.class);
        TreeNodeText treeText = this.field.getAnnotation(TreeNodeText.class);
        NameField nameField = this.field.getAnnotation(NameField.class);
        Title title = this.field.getAnnotation(Title.class);
        Dictionary dict = this.field.getAnnotation(Dictionary.class);
        Enumerated emed= this.field.getAnnotation(Enumerated.class);
        if(null != treeId || item.isPrimaryKey())
        {
            this.treeNodeType = TreeNodeTypes.Id;
        }
        else if(null != treePId)
        {
            this.treeNodeType = TreeNodeTypes.ParentId;
        }
        else if(null != treeText || null != nameField)
        {
            this.treeNodeType = TreeNodeTypes.Text;
        }
        if(null != title)
        {
            this.title = Algorithms.trimEx(title.value());
            this.width = Algorithms.trimEx(title.width());
        }
        if(this.treeNodeType != TreeNodeTypes.Id && null != field && field.getAnnotation(javax.persistence.Id.class) != null)
        {
            this.treeNodeType = TreeNodeTypes.Id;
        }
        if(null != dict)
        {
            this.dictTypeId =Algorithms.oneOfNotNull(
                 Algorithms.trimEx(dict.dictTypeId()),
                  Algorithms.trimEx(dict.value()));
            this.dictCategory = Algorithms.trimEx(dict.dictCategory());
            this.pojo = Algorithms.trimEx(dict.pojo());
            this.dictItems = Algorithms.trimEx(dict.items());
            this.enumName =dict.enumName();

            if(Algorithms.isEmpty(this.width))
            {
                this.width = Algorithms.trimEx(dict.width());
            }
            if(Algorithms.isEmpty(this.title))
            {
                this.title = Algorithms.trimEx(dict.title());
            }
            if(Algorithms.notEmpty(this.dictTypeId) || Algorithms.notEmpty(this.pojo))
            {
                dictionaryNode = true;
            }
        }
        //By default, treat all enum instances as dictinary.
        if(null != field && field.getType().isEnum())
        {
            if(null!=emed)
            {
                this.enumName=emed.value()==EnumType.STRING;
            }
            dictionaryNode = true;
        }
    }

    public boolean isDictionaryNode()
    {
        return this.dictionaryNode;
    }

    public boolean isTreeNode()
    {
        return this.treeNodeType != null && this.treeNodeType != TreeNodeTypes.None;
    }

    public String getDictTypeId()
    {
        return this.dictTypeId;
    }

    public String getDictCategory()
    {
        return this.dictCategory;
    }

    public String getPojo()
    {
        return this.pojo;
    }

    public String getTitle()
    {
        return this.title;
    }

    public String getWidth()
    {
        return this.width;
    }

    public String getDictItems()
    {
        return dictItems;
    }

    public TreeNodeTypes getTreeNodeType()
    {
        return treeNodeType;
    }

    public boolean isEnumName()
    {
        return enumName;
    }

    public Field getField()
    {
        return field;
    }

    public boolean isSortField()
    {
        return sortField;
    }

    public boolean isEnumDictinary()
    {
        return Algorithms.isEmpty(this.dictTypeId) && Algorithms.isEmpty(this.pojo)
            && this.field != null && this.field.getType().isEnum();
    }
}
